package menu

import (
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/admin"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

func MenuMy(c *gin.Context) {

	requestDataStruct := struct {
		Id int `json:"id"`
	}{}
	c.ShouldBindJSON(&requestDataStruct)

	//查询信息
	var adminGroupStruct admin.AdminGroupStruct

	model.GetModelDataFirst(
		model.Db,
		adminGroupStruct.TableName(),
		&adminGroupStruct,
		model.QueryOptions{
			Conditions: map[string]interface{}{
				"id": requestDataStruct.Id,
			},
			OrderBy: "id desc",
			Debug:   true,
		},
	)

	returnInt := []int{}
	if adminGroupStruct.Rules == "*" {
		var adminAuthRuleStruct admin.AdminAuthRuleStruct
		var adminAuthRuleData []admin.AdminAuthRuleStruct

		model.GetModelData(
			model.Db,
			adminAuthRuleStruct.TableName(),
			&adminAuthRuleData,
			model.QueryOptions{
				Conditions: map[string]interface{}{},
			},
		)

		for _, v := range adminAuthRuleData {
			returnInt = append(returnInt, v.Id)
		}
	} else {

		rules := strings.Split(adminGroupStruct.Rules, ",")
		for _, v := range rules {
			vInt, _ := strconv.Atoi(v)
			returnInt = append(returnInt, vInt)
		}
	}

	controller.Success(c, returnInt)
}
